package com.cskaoyan.gateway.controller.shopping;

import com.mall.commons.result.ResponseData;
import com.mall.commons.result.ResponseUtil;
import com.mall.shopping.ICartService;
import com.mall.shopping.dto.CheckAllItemRequest;
import com.mall.shopping.dto.CheckAllItemResponse;
import com.mall.shopping.dto.DeleteCheckedItemRequest;
import com.mall.shopping.dto.DeleteCheckedItemResposne;
import com.mall.user.constants.SysRetCodeConstants;
import lombok.extern.slf4j.Slf4j;
import org.apache.dubbo.config.annotation.Reference;
import org.springframework.web.bind.annotation.*;

@Slf4j
@RestController
@RequestMapping("shopping/items")
//@Api(tags = "ItemsController", description = "购物车删除控制层")
public class ItemsController {

    @Reference(check = false)
    ICartService iCartService;

    @DeleteMapping("{id}")
    public ResponseData<String> checkedItemDelete(@PathVariable long id) {
        DeleteCheckedItemRequest request = new DeleteCheckedItemRequest();
        request.setUserId(id);
        DeleteCheckedItemResposne response = iCartService.deleteCheckedItem(request);
        return SysRetCodeConstants.SUCCESS.getCode().equals(response.getCode())
                ? new ResponseUtil<String>().setData(SysRetCodeConstants.SUCCESS.getMessage())
                : new ResponseUtil<String>().setErrorMsg(response.getMsg());
    }

    @PutMapping()
    public ResponseData<String> checkAllItem(@RequestBody CheckAllItemRequest request) {
        if (!("true".equals(request.getChecked()) || "false".equals(request.getChecked()))) {
            return new ResponseUtil<String>().setErrorMsg(SysRetCodeConstants.REQUISITE_PARAMETER_NOT_EXIST.getMessage());
        }
        CheckAllItemResponse response = iCartService.checkAllCartItem(request);
        return SysRetCodeConstants.SUCCESS.getCode().equals(response.getCode())
                ? new ResponseUtil<String>().setData(SysRetCodeConstants.SUCCESS.getMessage())
                : new ResponseUtil<String>().setErrorMsg(response.getMsg());
    }
}
